//
//  CXYShopViewController.swift
//  诚叙缘
//
//  Created by sujeking on 17/3/20.
//  Copyright © 2017年 szw. All rights reserved.
//

import UIKit

class CXYShopViewController: CXYBaseTableViewController {
    
    var dataSource:Array<Any>!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        self.dataSource = Array()
        
        fetchItemList(name: "鲜花")
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    override func setupUI() {
        self.title = "礼物"
        
        self.automaticallyAdjustsScrollViewInsets = false
        
        let app = UIApplication.shared.delegate as! AppDelegate
        app.mainViewController?.setModuleType(moduleType: 2)
        
        let lineView = UINib(nibName: "CXYShopBottmView", bundle: nil).instantiate(withOwner: self, options: nil)[0] as! UIView
        let menuView = UINib(nibName: "CXYMenuView", bundle: nil).instantiate(withOwner: self, options: nil)[0] as! CXYMenuView
        
        let footView = UIView(frame: CGRect(x: 0, y: 0, width:UIScreen.main.bounds.width, height: 50))
        lineView.frame = footView.bounds
        footView.addSubview(lineView)
        
        
        
        menuView.delegate = self;
        let headView = UIView(frame: CGRect(x: 0, y: 0, width:UIScreen.main.bounds.width, height: 40))
        menuView.frame = headView.bounds
        headView.addSubview(menuView)
        
        self.tableView.tableHeaderView = headView
        self.tableView.tableFooterView = footView
        self.tableView.register(UINib.init(nibName: "CXYShopSingleCell", bundle: nil), forCellReuseIdentifier: "CXYShopSingleCell")
        self.tableView.register(UINib.init(nibName: "CXYShopDoubleTableViewCell", bundle: nil), forCellReuseIdentifier: "CXYShopDoubleTableViewCell")
        self.tableView.register(UINib.init(nibName: "CXYShopThreeTableViewCell", bundle: nil), forCellReuseIdentifier: "CXYShopThreeTableViewCell")
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.dataSource != nil {
            return self.dataSource.count
        }
        
        return 0;
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let row = indexPath.row
        let dict = self.dataSource[row] as! Dictionary<String,Any>
        
        let index = row + 1
        let cell : CXYShopBaseCell!
        
        if index % 2 == 1 {
            cell = tableView.dequeueReusableCell(withIdentifier: "CXYShopSingleCell", for: indexPath) as! CXYShopSingleCell
            cell.mainImageURL = dict["pict_url"] as! String!
        } else {
            let i = index / 2
            if i % 2 == 1 {
                cell = tableView.dequeueReusableCell(withIdentifier: "CXYShopThreeTableViewCell", for: indexPath) as! CXYShopThreeTableViewCell //3
                let imagesDict = dict["small_images"] as! Dictionary<String, Any>
                let images = imagesDict["string"] as! Array<Any>
                cell.leftImageURL = images.first as! String
                if images.count > 3 {
                    cell.rightTopImageURL = images[1] as! String
                } else {
                    cell.rightTopImageURL = images.first as! String
                }
                cell.rightBottomImageURL = images.last as! String
            } else {
                cell = tableView.dequeueReusableCell(withIdentifier: "CXYShopDoubleTableViewCell", for: indexPath) as! CXYShopDoubleTableViewCell //2
                let imagesDict = dict["small_images"] as! Dictionary<String, Any>
                let images = imagesDict["string"] as! Array<Any>
                cell.leftImageURL = images.first as! String
                cell.rightImageURL = images.last as! String
            }
        }
        return cell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let row = indexPath.row
        let index = row + 1
        
        if index % 2 == 1 {
            return 200
        } else {
            let i = index / 2
            if i % 2 == 1 {
                return 280
            } else {
                return 120
            }
        }
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        super.tableView(tableView, didSelectRowAt: indexPath)
        let row = indexPath.row
        let dict = self.dataSource[row] as! Dictionary<String,Any>
        let vc :CXYWebVC = CXYWebVC(nibName: "CXYWebVC", bundle: nil)
        let url = dict["item_url"] as! String
        let title = dict["title"] as! String
        vc.configWebVC(url: url,title: title)
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

extension CXYShopViewController:CXYMenuViewDelegate {
    func selectGatergoryItem(index: Int) {
        switch index {
        case 0:
            self.fetchItemList(name: "鲜花")
            break
            
        case 1:
            self.fetchItemList(name: "巧克力")
            break
            
        case 2:
            self.fetchItemList(name: "布娃娃")
            break
            
        case 3:
            self.fetchItemList(name: "配饰")
            break
            
        case 4:
            self.fetchItemList(name: "包包")
            break
            
        default: break
            
        }
    }
}

extension CXYShopViewController {
    func fetchItemList(name:String!) {
        CXYNetWork.getTaoBaoItemList(name: name) { (data) in
            let dict = data as Dictionary<String, Any>
            let ddd = dict["tbk_item_get_response"] as! Dictionary<String, Any>
            self.dataSource = (ddd["results"] as! Dictionary<String, Any>)["n_tbk_item"] as! Array<Any>!
            self.tableView.reloadData()
        }
    }
}






















































































